package com.alcatel_lucent.osp.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.alcatel_lucent.osp.model.Attachment;
import com.alcatel_lucent.osp.model.RequestStatus;
import com.alcatel_lucent.osp.service.RequestService;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("serial")
public class UpdateRequestAction extends JSONAction implements SessionAware,ServletRequestAware {
	

	private RequestService requestService;
	
	private Long request;
	private String comment;
	private String newStatus;
	private String targetDate;

	private HttpServletRequest httpRequest;

	@Override
	protected String performAction() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date td = targetDate == null || targetDate.trim().isEmpty() ? null : sdf.parse(targetDate);

        String[] copyList = httpRequest.getParameterValues("copyList[]");
        requestService.updateRequest(request, RequestStatus.valueOf(newStatus), td, comment, copyList);

		return SUCCESS;
	}

	public RequestService getRequestService() {
		return requestService;
	}

	@Autowired
	public void setRequestService(RequestService requestService) {
		this.requestService = requestService;
		this.requestService.setWebRoot(ServletActionContext.getServletContext().getRealPath("/"));
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getNewStatus() {
		return newStatus;
	}

	public void setNewStatus(String newStatus) {
		this.newStatus = newStatus;
	}

	public Long getRequest() {
		return request;
	}

	public void setRequest(Long request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
	}

	public String getTargetDate() {
		return targetDate;
	}

	public void setTargetDate(String targetDate) {
		this.targetDate = targetDate;
	}

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.httpRequest = request;
    }
}
